package com.uchedao.seller.ui.car.blue_tooth;

import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.uchedao.seller.inf.ILazyLoad;
import com.uchedao.seller.ui.car.base.BaseActivity;
import com.uchedao.seller.widget.loadstate.base.ILoadState;
import com.uchedao.seller.widget.loadstate.base.LoadstatueViewFactory;

/**
 * Description:
 * Date: 2015-09-30  11:04
 * User: wushan
 */
public abstract class CJDBaseHttpActivity extends BaseActivity implements ILoadState, ILazyLoad {

    private View mLoadingView;
    private View mLoadFailedView;
    protected View mContentView;
    private View mEmptyView;
    private boolean isInit = false;
    private boolean isLoad = true;// 是否第一次加载

    private Handler mHandler = new Handler() {

        @Override
        public void dispatchMessage(Message msg) {
            switch (msg.what) {
                case ILoadState.MESSAGE_ID_COMMON_LOADDATA_EMPTY:
                    mLoadingView.setVisibility(View.GONE);
                    mLoadFailedView.setVisibility(View.GONE);
                    mContentView.setVisibility(View.GONE);
                    mEmptyView.setVisibility(View.VISIBLE);
                    break;
                case ILoadState.MESSAGE_ID_COMMON_LOADDATA_FAILED:
                    mLoadingView.setVisibility(View.GONE);
                    mLoadFailedView.setVisibility(View.VISIBLE);
                    mContentView.setVisibility(View.GONE);
                    mEmptyView.setVisibility(View.GONE);
                    break;
                case ILoadState.MESSAGE_ID_COMMON_LOADDATA_FINISH:
                    invalidateViews();
                    mLoadingView.setVisibility(View.GONE);
                    mLoadFailedView.setVisibility(View.GONE);
                    mContentView.setVisibility(View.VISIBLE);
                    mEmptyView.setVisibility(View.GONE);
                    break;
                case ILoadState.MESSAGE_ID_COMMON_LOADDATA_START:
                    mLoadingView.setVisibility(View.VISIBLE);
                    mLoadFailedView.setVisibility(View.GONE);
                    mContentView.setVisibility(View.GONE);
                    mEmptyView.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        }

        ;
    };


    @Override
    protected void init() {
        super.init();
        initStateViews();
    }

    public void initStateViews() {
        if (isInit)
            return;
        mContentView = getContentView();
        mLoadingView = getLoadingView();
        mLoadFailedView = getLoadFailedView();
        mEmptyView = getEmptyView();
        isInit = true;
    }

    /**
     * 刷新视图
     */
    @Override
    public abstract void invalidateViews();

    @Override
    public void onLoadStart() {
        mHandler.sendEmptyMessage(ILoadState.MESSAGE_ID_COMMON_LOADDATA_START);
    }

    @Override
    public void onLoadSuccess() {
        isLoad = false;
        mHandler.sendEmptyMessage(ILoadState.MESSAGE_ID_COMMON_LOADDATA_FINISH);
    }

    @Override
    public void onLoadFailed() {
        isLoad = true;
        mHandler.sendEmptyMessage(ILoadState.MESSAGE_ID_COMMON_LOADDATA_FAILED);
    }

    @Override
    public void onLoadEmpty() {
        isLoad = true;
        mHandler.sendEmptyMessage(ILoadState.MESSAGE_ID_COMMON_LOADDATA_EMPTY);
    }

    @Override
    public boolean firstdata() {
        if (isLoad) {
            onLoadStart();
            loadData(1);
            return true;
        }
        return false;

    }


    /**
     * 获取加载中的图
     *
     * @return
     */
    @Override
    public View getLoadingView() {
        if (mContentView != null) {
            return LoadstatueViewFactory.getLoadingView(this, mContentView, "");
        }
        return null;
    }

    ;

    /***
     * 获取加载失败的图
     *
     * @return
     */
    @Override
    public View getLoadFailedView() {
        if (mContentView != null) {
            return LoadstatueViewFactory.getLoadingFailed(this, mContentView,
                    new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            firstdata();
                        }
                    });
        }
        return null;
    }

    /**
     * 获取空数据视图
     *
     * @return
     */
    @Override
    public View getEmptyView() {
        if (mContentView != null) {
            return LoadstatueViewFactory.getLoadEmpty(this, mContentView,
                    new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            firstdata();
                        }
                    });
        }
        return null;
    }

}
